package com.dycong.common.threadUtil;

import java.util.concurrent.*;

/**
 * Created by dycong on 2016/11/16.
 */
public class TestFuture {
    // TODO: 2016/11/17 这些都需要在main()方法运行，创建POOL实例，Junit是单元测试，不适合在这里测试，不靠谱，处理不好关联关系吧？！
    static ExecutorService executorService;

    static {
        executorService = Executors.newFixedThreadPool(20);
    }


    public void init() {
        String str = "";

        FutureTask<String> futureTask = new FutureTask<String>(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println(Thread.currentThread().getName());
                        Thread.sleep(1000);
                    System.out.println("R");
                } catch (Exception e) {
                    System.out.println("ERROR");
                }
            }
        }, str);
        FutureTask<String> futureTask1 = new FutureTask<String>(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }, str);

        executorService.submit(futureTask);

//        executorService.submit(futureTask1);


    }


    public void init1() {

        FutureTask<String> futureTask = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(500);
                System.out.println(Thread.currentThread().getName());
                return "ok";
            }
        });

        FutureTask<String> futureTask1 = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                return "";
            }
        });


        executorService.submit(futureTask);
        try {
            executorService.submit(futureTask1).get();
        }catch (ExecutionException e){
            e.printStackTrace();
        }catch (InterruptedException te){
            te.printStackTrace();
        }

        try {
            System.out.println(futureTask.get());

        } catch (Exception e) {

        }
    }




    public void test() {
        executorService.submit(new Callable<String>() {

            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                Thread.sleep(1000);
                System.out.println("123");

                return "";
            }
        });

        executorService.submit(new Callable<String>() {

            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName());
                System.out.println("456");

                return "";
            }
        });
    }

    public static void main(String[] args) {
        TestFuture future = new TestFuture();

        for (int i = 0; i < 20; i++) {
//            future.init1();
//            future.test();
            future.init1();
        }
    }
}
